package com.mj.mafeed.activity;

import java.util.ArrayList;
import java.util.List;

import com.mj.mafeed.R;
import com.mj.mafeed.adapter.ReceiverSelectAdapter;
import com.mj.mafeed.bean.ReceiverJSON;
import com.mj.mafeed.bean.ReceiverListJSON;
import com.mj.mafeed.service.ReceiverService;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.LoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 选择收货地址
 * @author zhaominglei
 * @date 2014-12-17
 * 
 */
public class ReceiverSelectActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = ReceiverSelectActivity.class.getSimpleName();
	private ReceiverService receiverService = new ReceiverService();
	private ImageView goHome;
	private Button editBtn;
	private LoadingDialog loadingDialog; // 加载框
	private ListView receiverItemView;
	private ReceiverSelectAdapter receiverSelectAdapter;
	private List<ReceiverJSON> receivers = new ArrayList<ReceiverJSON>();
	private String referer; // 来自那个链接
	private String username; //用户名
	private String sign; //签名
	private Intent intent;
	private ReceiverJSON selectedReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_receiver_select);
		
		init();
	}

	private void init() {
		goHome = (ImageView) findViewById(R.id.receiver_select_gohome);
		goHome.setVisibility(View.INVISIBLE);
		editBtn = (Button) findViewById(R.id.receiver_select_edit);
		//TODO 加上编辑
		editBtn.setVisibility(View.INVISIBLE);
		receiverItemView = (ListView) findViewById(R.id.receiver_select_listview);
		
		goHome.setOnClickListener(this);
		editBtn.setOnClickListener(this);
		receiverSelectAdapter = new ReceiverSelectAdapter(this, receivers);
		receiverItemView.setAdapter(receiverSelectAdapter);
		loadingDialog = new LoadingDialog(this);
		
		referer = getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		selectedReceiver = (ReceiverJSON)getIntent().getSerializableExtra("selectedReceiver");
		
		receiverItemView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				RelativeLayout relativeLayout = (RelativeLayout)view;
				Long descId = (Long)relativeLayout.getTag();
				ReceiverJSON receiver = null;
				receiverSelectAdapter.getIsSelected().clear();
				for (ReceiverJSON receiverJSON : receivers) {
					if (receiverJSON.getReceiverId().equals(descId)) {
						receiverSelectAdapter.getIsSelected().put(descId, true);
						receiver = receiverJSON;
					} else {
						receiverSelectAdapter.getIsSelected().put(receiverJSON.getReceiverId(), false);
					}
				}
				receiverSelectAdapter.notifyDataSetChanged();
				intent = new Intent(ReceiverSelectActivity.this, CheckoutActivity.class);
				intent.putExtra("referer", "ReceiverSelectActivity");
				Bundle bundle = new Bundle();
				bundle.putSerializable("receiver", receiver);
				intent.putExtras(bundle);
				startActivity(intent);
				ReceiverSelectActivity.this.finish();
			}
		});
		getReceivers();
	}

	private void getReceivers() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new ReceiverListAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.receiver_list_gohome:
			finish();
			break;
			
		case R.id.receiver_list_add:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				Intent intent = new Intent(getApplicationContext(), ReceiverDetailActivity.class);
				intent.putExtra("referer", "ReceiverListActivity");
				intent.putExtra("action", "add");
				startActivity(intent);
				finish();
			} else {
				Toast.makeText(getApplicationContext(), R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
			
		default:
			break;
		}
	}
	
	public class ReceiverListAsyncTask extends AsyncTask<String, String, ReceiverListJSON> {

		@Override
		protected ReceiverListJSON doInBackground(String... params) {
			return receiverService.getReceiver(referer, username, sign);
		}

		@Override
		protected void onPostExecute(ReceiverListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				receivers = result.getRecordList();
				receiverSelectAdapter.setReceivers(result.getRecordList());
				if (selectedReceiver != null) {
					receiverSelectAdapter.getIsSelected().put(selectedReceiver.getReceiverId(), true);
				} else {
					for (ReceiverJSON receiverJSON : receivers) {
			        	if (receiverJSON.getIsDefault() != null && receiverJSON.getIsDefault()) {
			        		receiverSelectAdapter.getIsSelected().put(receiverJSON.getReceiverId(), true);
			        		break;
			        	}
					}
				}
				receiverSelectAdapter.notifyDataSetChanged();
			}
		}
	}
}
